package talkplace;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.io.IOUtils;
import talkplace.mail;
import talkplace.man;


public class server {

	public static synchronized void check(int request,Socket socket,mail mail) throws IOException {
		
		    if(request==0)
		    {listman.add( id++,new man(id,new Date())); mapman.add(id);
		    action_list(socket,mail);}
		    else if(request==2)action_input(socket,mail);
		    else if(request==1)
		    	{
		    	action_list(socket,mail);
		    	}
		    else if(request==3)action_area(socket,mail);
		    else 
		    {   
		    	socket.close();
		    }
			Date time=new Date();
	    	for(int i=0;i<listman.size();i++)
	    	   if(Math.abs(time.getTime()-listman.get(i).getData().getTime())>2000)				    	   
	    	   {  mapman.remove(listman.get(i).getPlace()-1); 
	    	   listman.get(i).setPlace(-1);	
	    	   
	    	   }	
	   
	}
	public static void action_list(Socket socket,mail mail) {
		pool.submit(new Runnable() {
			@Override
			public void run() {
				try {	
					
			    	
			       if (mail.getPlace()>0&&mapman.add(mail.getPlace()))
			    	   
			    	   listman.add(mail.getPlace(),new man(mail.getPlace(),new Date()));
			       else if(mail.getPlace()>0&&!mapman.add(mail.getPlace()))
			    	   
			      
			       listman.get(mail.getPlace()-1).setData(new Date());
			       
			       
			     //  System.out.println(mail.put(id,listman));
			    	   socket.getOutputStream().write(mail.put(id,listman).getBytes());
			    	   
		
				}catch(IOException e) {e.printStackTrace();}
				finally {
					try {
								    	   
					    	
					socket.close();
				
				} catch (IOException e) {
				// TODO Auto-generated catch block
					e.printStackTrace();
				}
					}
			}
		});
	}
	public static void action_input(Socket socket,mail mail) {
		pool.submit(new Runnable() {

			@Override
			public void run() {
			String[]st=mail.re();
					try {
				Date temp = simpleDateFormat.parse(st[0]);
				inputmap.put(temp, st[1]);
			
				} catch (ParseException e1) {
					e1.printStackTrace();
				}
				
			}
		});
	}
	public static void action_area(Socket socket,mail mail) {
		pool.submit(new Runnable() {

			@Override
			public void run() {
				
			
				String[]st=mail.re();
		
				try {
				//	System.out.println(inputmap.toString());
				Date temp = simpleDateFormat.parse(st[0]);
			inputmap.forEach((time,value)->{
			
				if(Math.abs(time.getTime()-temp.getTime())<=2000)
				 {
			
						try {
						
							socket.getOutputStream().write((simpleDateFormat.format(time)+" "+value+"\n").getBytes());
						} catch (IOException e) {
						
							e.printStackTrace();
						}
					} 
				else if(Math.abs(time.getTime()-temp.getTime())>4000)
					inputmap.remove(time, value);
				
			});
			
				} catch (ParseException e1) {
					
					e1.printStackTrace();
				}
			finally {try {
				socket.close();
			} catch (IOException e) {
				
				e.printStackTrace();
			}}	
			}
		});
	}
	static ServerSocket server;
	static int ip=8888;
	static ExecutorService pool=Executors.newCachedThreadPool();
	static 	ArrayList<man >listman=new ArrayList<man>(20);
	static HashSet<Integer>mapman=new HashSet<Integer>();
	static HashMap<Date,String>inputmap=new HashMap<Date,String>();
	static SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	static int id=0;
	public static void main(String[] args) throws IOException {
	ServerSocket server=new ServerSocket(ip);
	ArrayList<man >listman=new ArrayList<man>(20);
	while(true) {
		Socket socket=server.accept();
		
		String get=IOUtils.toString(socket.getInputStream(),"utf-8");

	
		
		mail mail=new mail(get.toString());
		
		check(mail.getRequest(),socket,mail);

	}
}
	}
